import { _decorator, AnimationComponent, Component, Node } from 'cc';
import { clientEvent } from '../../framework/clientEvent';
import { constant } from '../../framework/constant';
import { gameManager } from '../../fight/gameManager';
import { uiManager } from '../../framework/uiManager';
const { ccclass, property } = _decorator;

@ccclass('loadingPanel')
export class loadingPanel extends Component {
    @property(AnimationComponent)
    // 动画组件
    public aniCloud: AnimationComponent = null;


    onEnable() {
        clientEvent.on(constant.EVENT_TYPE.SHOW_LOADING_PANEL, this._showLoadingPanel, this);
        clientEvent.on(constant.EVENT_TYPE.HIDE_LOADING_PANEL, this._hideLoadingPanel, this);
    }

    onDisable() {
        clientEvent.off(constant.EVENT_TYPE.SHOW_LOADING_PANEL, this._showLoadingPanel, this);
        clientEvent.off(constant.EVENT_TYPE.HIDE_LOADING_PANEL, this._hideLoadingPanel, this);
    }


    /** 显示加载面板动画 */
    private _showLoadingPanel() {
        this.aniCloud.getState("cloudAnimationIn").time = 0;
        this.aniCloud.getState("cloudAnimationIn").sample();
        this.aniCloud.play("cloudAnimationIn");
    }

    /** 隐藏加载面板动画 */
    private _hideLoadingPanel() {
        console.log('gameManager',gameManager)
        gameManager.scriptGameCamera.resetCamera();
        this.aniCloud.getState("cloudAnimationOut").time = 0;
        this.aniCloud.getState("cloudAnimationOut").sample();
        this.aniCloud.play("cloudAnimationOut");
        this.aniCloud.once(AnimationComponent.EventType.FINISHED, () => {
            console.log('隐藏加载面板动画')
            uiManager.instance.hideDialog("loading/loadingPanel");
            uiManager.instance.showDialog("fight/fightPanel", [this]);
        });
    }

    public show () {
        this._showLoadingPanel();
    }

    update(deltaTime: number) {

    }
}


